package com.ruoyi.biz.letter.read;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author Fei.Cong
 * @date 2024-07-18 15:27
 */
public class EmergencyConverter implements Converter<String> {
    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        String value = "";
        List<SysDictData> dictData = DictUtils.getDictCache("biz_hotline_emergency");
        Optional<SysDictData> data = dictData.stream().filter(v -> Objects.equals(v.getDictLabel(), cellData.getStringValue())).findFirst();
        if (data.isPresent()) {
            value = data.get().getDictValue();
        }
        return value;
    }

    @Override
    public WriteCellData convertToExcelData(String value, ExcelContentProperty contentProperty,
                                            GlobalConfiguration globalConfiguration) {
        String str = "";
        List<SysDictData> dictData = DictUtils.getDictCache("biz_hotline_emergency");
        Optional<SysDictData> data = dictData.stream().filter(v -> Objects.equals(v.getDictValue(), value)).findFirst();
        if (data.isPresent()) {
            str = data.get().getDictLabel();
        }
        return new WriteCellData(str);
    }
}